﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Transactions;
using Core.Contract.DataContract;
using Core.DAO;
using Data.BO;
using www.Models;

namespace Controllerss
{
    public class AdvertiseController
    {
        #region value
        private readonly AdvertiseRefBO _advertiseBo = new AdvertiseRefBO();
        private readonly AdvertiseValueBO _advertiseValueBo=new AdvertiseValueBO();
        #endregion

        #region Ham lay du lieu

        public IQueryable<RelaxAdvertiseDataContract> GetAdvertiseDataContracts()
        {
            //using (var scope=new DAOScope())
            using (var scope = new TransactionScope())
            {
                var lReturn = _advertiseBo.GetAdvertiseDataContracts();
                scope.Complete();
                return lReturn;
            }
        }
        #endregion

        #region Add

        public long Add(List<RelaxAdvertiseDataContract> advertiseDataContract)
        {
            using (var scope = new TransactionScope())
            {
                var iD = 0;
                var avertise = new ADVERTISE_REF
                {
                    ord = advertiseDataContract[0].Ord,
                    type = advertiseDataContract[0].Type
                };
                _advertiseBo.Add(avertise);
                var first = _advertiseBo.GetDataItem().OrderByDescending(o => o.Id).FirstOrDefault();
                if (first != null)
                {
                    iD = Convert.ToInt32(first.Id);
                    foreach (var item in advertiseDataContract)
                    {
                        var advertiseValue = new ADVERTISE_VALUE
                        {
                            advertiseid = iD,
                            languagecode = item.Languagecode,
                            name = item.Name,
                            image = item.Image,
                            link = item.Link,
                            target = item.Target,
                            decription = item.Decription,
                            Price = item.Price,
                            FromDate = item.FromDate,
                            ToDate = item.ToDate,
                            active = item.Active
                        };
                        _advertiseValueBo.Add(advertiseValue);
                    }
                }
                scope.Complete();
                return iD;
            }
        }
        #endregion

        #region Update
        public Boolean Update(List<RelaxAdvertiseDataContract> advertiseDataContract)
        {
            using (var scope = new TransactionScope())
            {
                
                var avertise = new ADVERTISE_REF
                {
                    id = Convert.ToInt32(advertiseDataContract[0].Id),
                    ord = advertiseDataContract[0].Ord,
                    type = advertiseDataContract[0].Type
                };
                _advertiseBo.Update(avertise);
                foreach (var item in advertiseDataContract)
                {
                    var advertiseValue = new ADVERTISE_VALUE
                    {
                        advertiseid = Convert.ToInt32(item.Id),
                        languagecode = item.Languagecode,
                        name = item.Name,
                        image = item.Image,
                        link = item.Link,
                        target = item.Target,
                        decription = item.Decription,
                        Price = item.Price,
                        FromDate = item.FromDate,
                        ToDate = item.ToDate,
                        active = item.Active
                    };
                    if (!_advertiseValueBo.Update(advertiseValue))
                    {
                        _advertiseValueBo.Add(advertiseValue);
                    }
                }
                scope.Complete();
                return true;
            }
        }
        #endregion

        #region Delete

        public Boolean Delete(List<long> lId,long iLanguageCode)
        {
            using (var scope = new TransactionScope())
            {
                foreach (var id in lId)
                {
                    _advertiseValueBo.Delete(id,iLanguageCode);
                }
                scope.Complete();
                return true;
            }
        }
        #endregion
    }
}
